﻿using System;
using System.Collections.Generic;
using System.Text;
using POSPlus.EZBarTender.DatabaseOperations;
using System.Data;
using System.Data.SqlClient;

namespace POSPlus.PrintManager
{
	public class EdlpInfo 
	{
		public static DataTable GetAllEdlp(string connectionString)
		{
			DataTable results = new DataTable("All Edlp");
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("EXECUTE sp_AllEdlps", connection))
				{
					//command.CommandType = CommandType.StoredProcedure;
					using (SqlDataAdapter adapter = new SqlDataAdapter(command))
					{
						adapter.Fill(results);
					}
				}
				connection.Close();
			}
			return results;
		}

		public static DataTable GetNewEdlp(string connectionString)
		{
			DataTable results = new DataTable("New Edlp");
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("EXECUTE sp_NonPrintedEdlps", connection))
				{
					using (SqlDataAdapter adapter = new SqlDataAdapter(command))
					{
						adapter.Fill(results);
					}
				}
				connection.Close();
			}
			return results;
		}

		internal static void ClearTearList(string connectionString)
		{
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("EXECUTE sp_ClearEdlpTearList", connection))
				{
					command.ExecuteNonQuery();
				}
			}
		}

		internal static void SetNewAsPrinted(string connectionString)
		{
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("EXECUTE sp_SetEdlpPrintedDate", connection))
				{
					command.ExecuteNonQuery();
				}
			}
		}

		public static int GetEdlpStatus(string upc, string connectionString)
		{
			int result = 0;
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("sp_GetEdlpStatus", connection))
				{
					command.CommandType = CommandType.StoredProcedure;
					command.Parameters.Add(new SqlParameter("@Upc", upc));

					SqlParameter returnValue = new SqlParameter("@ReturnValue", DbType.Int32);
					returnValue.Direction = ParameterDirection.ReturnValue;

					command.Parameters.Add(returnValue);

					command.ExecuteNonQuery();

					result = Int32.Parse(command.Parameters["@ReturnValue"].Value.ToString());
				}
				connection.Close();
			}
			return result;
		}

		public static int UpcExists(string upc, string connectionString)
		{
			int result = 0;
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("sp_UpcExists", connection))
				{
					command.CommandType = CommandType.StoredProcedure;
					command.Parameters.Add(new SqlParameter("@Upc", upc));

					SqlParameter returnValue = new SqlParameter("@ReturnValue", DbType.Int32);
					returnValue.Direction = ParameterDirection.ReturnValue;

					command.Parameters.Add(returnValue);

					command.ExecuteNonQuery();

					result = int.Parse(command.Parameters["@ReturnValue"].Value.ToString());
				}
				connection.Close();
			}
			return result;
		}
	}
}